<template>
  <div>
    <avue-crud
      :option="option"
      :data="data"
      :table-loading="option.loading"
      :permission="permissionList"
      v-model:page="page"
      @row-save="save"
      @row-del="del"
      @row-update="update"
      @on-load="init"
      @search-change="search"
      @refresh-change="refresh"
      @selection-change="selectChange"
      v-model="form"
    >
      <template #status="{ row }">
        <el-switch
          v-model="row.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="changeStatus($event, row)"
        >
        </el-switch>
      </template>
      <template #menu-left="{ size }">
        <el-button
          type="danger"
          :size="size"
          icon="el-icon-delete"
          v-if="!this.validatenull(this.permission.SysResource_remove)"
          @click="delBatch"
          >批量删除
        </el-button>
      </template>
    </avue-crud>
  </div>
</template>
<script>
import {
  list,
  pageList,
  query,
  save,
  update,
  remove,
  switchStart,
} from "@/api/admin/SysResourceApi.js";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      data: [],
      form: {},
      params: {},
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      selectionList: [],
      option: {
        loading: true,
        selection: true,
        searchMenuSpan: 4,
        height: "auto",
        index: true,
        column: {
          name: {
            label: "资源名称",
            type: "select",
            span: 24,
            props: {
              label: "dictName",
              value: "dictValue",
            },
            dicUrl: `/admin/sysDict/upload_resource`,
            rules: [
              {
                required: true,
                message: "请输入资源名称",
                trigger: "blur",
              },
            ],
          },
          key: {
            label: "appkey",
            type: "password",
            rules: [
              {
                required: true,
                message: "请输入appkey",
                trigger: "blur",
              },
            ],
          },
          secret: {
            label: "密钥",
            type: "password",
            rules: [
              {
                required: true,
                message: "请输入密钥",
                trigger: "blur",
              },
            ],
          },
          endpoint: {
            label: "域名",
            rules: [
              {
                required: true,
                message: "请输入区域",
                trigger: "blur",
              },
            ],
          },
          bucket: {
            label: "bucket",
            rules: [
              {
                required: true,
                message: "请输入存储空间",
                trigger: "blur",
              },
            ],
          },
          status: {
            label: "是否开启",
            type: "radio",
            value: false,
            dicData: [
              { label: "是", value: true },
              { label: "否", value: false },
            ],
            rules: [
              {
                required: true,
                message: "请输入是否开启",
                trigger: "blur",
              },
            ],
          },
        },
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    selectIds() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id + "");
      });
      return ids.join(",") + "";
    },
    permissionList() {
      return {
        addBtn: !this.validatenull(this.permission.SysResource_add),
        delBtn: !this.validatenull(this.permission.SysResource_remove),
        editBtn: !this.validatenull(this.permission.SysResource_edit),
      };
    },
  },
  methods: {
    changeStatus(val, row) {
      let data = {
        id: row.id,
        status: val,
      };
      switchStart(data).then((res) => {
        this.refreshLoad(res);
      });
    },
    init() {
      this.option.loading = true;
      pageList(this.page, this.params)
        .then((res) => {
          this.data = res.data.data.records;
          this.page.total = res.data.data.total;
          this.option.loading = false;
        })
        .catch(() => {
          this.option.loading = false;
        });
    },
    save(row, done, loading) {
      loading();
      save(row).then((res) => {
        this.refreshLoad(res);
      });
      done();
    },
    update(row, index, done, loading) {
      loading();
      update(row).then((res) => {
        this.refreshLoad(res);
      });
      done();
    },
    del(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then((res) => {
          this.refreshLoad(res);
        });
    },
    delBatch() {
      if (this.selectIds.length === 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.selectIds);
        })
        .then((res) => {
          this.refreshLoad(res);
        });
    },
    refreshLoad(res) {
      this.init();
      this.$message({
        type: "success",
        message: res.data.msg,
      });
    },
    refresh() {
      this.init();
    },
    search(form, done) {
      this.params = form;
      this.init();
      done();
    },
    selectChange(list) {
      this.selectionList = list;
    },
  },
};
</script>
<style scoped lang="scss"></style>
